package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MultiPaneWidth extends Activity {
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multipanewidth);
	}
	public static class PlanetListFragment extends ListFragment{
		int mLastIndex=0;
		boolean mMultiPane;
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			setListAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, PlanetInfo.PLANET));
			getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			
			if(savedInstanceState !=null){
				mLastIndex =savedInstanceState.getInt("lastindex");
			}
			
			View descPanel =getActivity().findViewById(R.id.descpanel);
			if(descPanel !=null && descPanel.getVisibility()==View.VISIBLE){
				//태블릿
				mMultiPane=true;
				onListItemClick(getListView(), null, mLastIndex, 0);
			}
		}//end of method onActivityCreated
		
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			outState.putInt("lastindex", mLastIndex);
			
		}
		public void onListItemClick(ListView l, View v, int position, long id) {
			super.onListItemClick(l, v, position, id);
			mLastIndex=position;
			getListView().setItemChecked(position, true);
			
			if(mMultiPane){
				//태블릿
			}else{
				Intent intent=new Intent(getActivity(), DescActivity.class);
				intent.putExtra("index", position);
				startActivity(intent);
				//핸드셋
			}
		}//end of method onListItemClick
		
		
	}//end of class PlanetListFragment
	public static class DescFragment extends Fragment{
		int mNowIndex;
		
		public static DescFragment newInstance(int index) {
			DescFragment df=new DescFragment();
			df.mNowIndex=index;
			return df;
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			return super.onCreateView(inflater, container, savedInstanceState);
		}
		
	}//end of class DescFragment
	public static class DescActivity extends Activity{
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			int index=getIntent().getExtras().getInt("index");
			DescFragment details=DescFragment.newInstance(index);
			getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
		}
	}//end of class DescActivity
}
